import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../provider/AuthProvider.dart';

class SettingsPage extends StatefulWidget {
  const SettingsPage({super.key});

  @override
  State<SettingsPage> createState() => _SettingsPageState();
}

class _SettingsPageState extends State<SettingsPage> {
  bool _isDarkMode = false; // 主题状态，实际项目可和全局状态绑定

  @override
  void initState() {
    super.initState();
    // 这里可以初始化主题状态，比如从全局状态读取
  }

  void _toggleTheme(bool value) {
    setState(() {
      _isDarkMode = value;
    });
    // TODO: 这里调用全局状态或系统主题切换方法，比如 Provider 通知App层主题变化
  }

  void _showAboutDialog() {
    showAboutDialog(
      context: context,
      applicationName: '小蓝书',
      applicationVersion: 'v1.0.0',
      applicationIcon: const Icon(Icons.apps, size: 40),
      children: const [
        Text('共享资源 互利互助'),
        Text('© 2025 个人开发'),
      ],
    );
  }

  void _navigateToHelpFeedback() {
    // 这里可以跳转到帮助反馈页面，示例中简单弹个对话框
    showDialog(
      context: context,
      builder: (_) => AlertDialog(
        title: const Text('帮助与反馈'),
        content: const Text('如需帮助，请联系2950790230@qq.com'),
        actions: [
          TextButton(
            onPressed: () => Navigator.pop(context),
            child: const Text('关闭'),
          )
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    final authProvider = context.read<AuthProvider>();

    return Scaffold(
      appBar: AppBar(title: const Text('设置')),
      body: ListView(
        children: [
          // 主题设置开关
          SwitchListTile(
            title: const Text('夜间模式'),
            secondary: const Icon(Icons.brightness_6),
            value: _isDarkMode,
            onChanged: _toggleTheme,
          ),

          // 关于App
          ListTile(
            leading: const Icon(Icons.info_outline),
            title: const Text('关于应用'),
            onTap: _showAboutDialog,
          ),

          // 帮助与反馈
          ListTile(
            leading: const Icon(Icons.help_outline),
            title: const Text('帮助与反馈'),
            onTap: _navigateToHelpFeedback,
          ),

          const Divider(),

          // 退出登录
          ListTile(
            title: const Text('退出登录'),
            leading: const Icon(Icons.logout, color: Colors.red),
            onTap: () async {
              await authProvider.logout();
              if (!context.mounted) return;
              Navigator.of(
                context,
              ).pushNamedAndRemoveUntil('/login', (r) => false);
            },
          ),
        ],
      ),
    );
  }
}
